﻿#pragma once
//实体类方法
#define IMETHODS(pobj,obj)\
typedef pobj PObj;\
typedef obj IObj;\
/*operator&重载*/\
IObj*operator&() { ((Object const*)this)->operator&(); return this; }\
IObj const*operator&()const {  ((Object const*)this)->operator&(); return this; }\
/*动态获取类型信息*/\
virtual ptr<Type> GetType()const{ return Type::Make<pobj>(); }\
virtual void SwapWith(PObject&){Throw<PNotSupportException >();}

#define BMETHODS(pobj,obj)\
typedef pobj PObj;\
typedef obj IObj;\
/*动态获取类型信息*/\
virtual ptr<Type> GetType()const{ return Type::Make<pobj>(); }\
/*operator&重载*/\
IObj*operator&() { ((Object const*)this)->operator&(); return this; }\
IObj const*operator&()const {  ((Object const*)this)->operator&(); return this; }\
virtual void SwapWith(PObject&o){SwapWith(*CastPointer<IObj>(o.Check()));}\
template<class TT>void SwapWith(TT&o){Object::Swap(*this,o);}


//代理类方法
#define PIMETHODS_NODEFCTOR(pobj,obj)\
typedef pobj PObj;\
typedef obj IObj;\
/**@brief 复制构造函数*/\
pobj(pobj const&t){ptr<Object>::operator=(t);}\
/**@brief 从对象指针构造*/\
pobj(obj const* p){ptr<Object>::SetTo(StaticCastPointer(p)); }\
/**@brief 从空指针*/\
pobj(null_t){}\
/**@brief 赋值*/\
pobj& operator=(pobj const&r) { ptr<Object>::operator=(r); return *this; }\
/**@brief 置空*/\
pobj& operator=(null_t) { ptr<Object>::SetTo(null); return *this; }\
bool operator==(null_t)const{return ptr<Object>::operator==(null);}\
bool operator!=(null_t)const{return ptr<Object>::operator!=(null);}\
/**@brief 获取对象指针，并在对象指针为空时抛出异常*/\
obj* Check()const { return CheckPointer<obj>(ptr<Object>::GetPtr()); }\
/**@brief 获取对象指针*/\
obj* GetPtr()const {return CastPointer<obj>(ptr<Object>::GetPtr());}\
/**@brief 重载operator->*/\
obj* operator->()const { return Check(); }


#define PIMETHODS(pobj,obj)\
PIMETHODS_NODEFCTOR(pobj,obj)\
/**@brief 默认构造函数*/\
inline pobj(){}

#define PMETHODS_NODEFCTOR(pobj,obj)\
/*operator*重载*/\
obj& operator*()const { if (UseCount<0)++UseCount; return *(obj*)Check(); }\
PIMETHODS_NODEFCTOR(pobj,obj)

#define PMETHODS(pobj,obj)\
PIMETHODS_NODEFCTOR(pobj,obj)\
/**@brief 默认构造函数*/\
inline pobj(){}

#define PTRPREDEFINE(CT,Type)\
template<CT>struct ptr<Type>:ptr<Object> {\
 PIMETHODS(ptr, Type)\
template<class OT>inline ptr(OT const&t, typename UseIf<IsBaseOf<Type, typename UseT<IsBaseOf<PObject, OT>::V, PObject, OT>::T::IObj>::V>::T*p = NULL)\
 { ptr<Object>::operator=(t); } };
